package com.me.mygdxgame.login;

import kryonetpackets.LoginRequest;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.InputEvent;
import com.badlogic.gdx.scenes.scene2d.InputListener;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.ui.TextField;
import com.badlogic.gdx.scenes.scene2d.utils.SpriteDrawable;
import com.me.mygdxgame.MyGdxGame;

public class LoginScreen implements Screen {
	private static final int MAX_USER_NAME_LENGTH = 20;
	private static final float BASE_MUSIC_VOLUME = 0.125f;
	
	private Skin fieldSkin;
	private TextField username, password;
	private TextButton loginButton;
	private Stage stage;
	
	private Music music = null;
	
	private final MyGdxGame game;

	public LoginScreen(OrthographicCamera c, final MyGdxGame game) {
		this.game = game;
		this.createMusic();
		
		Texture texture = new Texture(
				Gdx.files.internal("data/textures/LoginScreen.png"));
		texture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
		int startX = (texture.getWidth() - Gdx.graphics.getWidth()) / 2;
		int startY = (texture.getHeight() - Gdx.graphics.getHeight()) / 2;
		
		TextureRegion region = new TextureRegion(texture, startX, startY,
				Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
		Sprite background = new Sprite(region);
		
		this.stage = new Stage();
        Gdx.input.setInputProcessor(this.stage);

        Table table = new Table();
        table.setFillParent(true);
        table.setBackground(new SpriteDrawable(background));
        this.stage.addActor(table);
        
        table = new Table();
        table.setFillParent(true);
        table.right();
        table.top();
        table.pad(50f); //TODO: Add a background image instead of a pad
        this.stage.addActor(table);
		
        TextureAtlas textureAtlas = new TextureAtlas(
				Gdx.files.internal("data/uiskin.atlas"));
		this.fieldSkin = new Skin(Gdx.files.internal("data/uiskin.json"),
				textureAtlas);
		
		this.username = new TextField("", this.fieldSkin);
		this.username.toFront();
		this.username.setMaxLength(this.MAX_USER_NAME_LENGTH);
		this.username.setMessageText("User Name");
		table.row();
		table.add(this.username).space(10f);
		
		this.password = new TextField("", this.fieldSkin);
		this.password.toFront();
		this.password.setPasswordMode(true);
		this.password.setPasswordCharacter('*');
		this.password.setMessageText("Password");
		table.row();
		table.add(this.password).space(10f);
		
		this.loginButton = new TextButton("Login", this.fieldSkin);
		this.loginButton.addListener(new InputListener() {
			// Detect mouse press
			public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {
				if (username.getText() != null && password.getText() != null &&
						username.getText() != "" && password.getText() != "")
				{
					sendLoginPacket();
					return true;
				}
				return false;
			}
		});
		this.stage.addListener(new InputListener() {
			public boolean keyDown(InputEvent event, int keycode) {
				if (keycode == Keys.ENTER) {
					sendLoginPacket();
					return true;
			    }
			    return false;
			}
		});
		table.row();
		table.add(this.loginButton).space(10f).right();
		
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();
	}

	@Override
	public void resize(int width, int height) {
		this.stage.setViewport(width, height, true);
	}

	@Override
	public void show() {}

	@Override
	public void hide() {}

	@Override
	public void pause() {}

	@Override
	public void resume() {}

	@Override
	public void dispose() {
		if (this.music != null) {
			this.music.stop();
			this.music.dispose();
		}
		this.stage.dispose();
	}

	private void createMusic() {
		this.music = Gdx.audio.newMusic(
				Gdx.files.internal("data/music/IntroMusic.wav"));
		this.music.setLooping(true);
		//TODO: Multiply the base by the settings volume
		this.music.setVolume(BASE_MUSIC_VOLUME);
		this.music.play();
	}
	
	private void sendLoginPacket() {
		game.getGameClient().sendPacket(
				new LoginRequest(username.getText(),password.getText()));
	}
}
